import { Injectable } from '@nestjs/common';
import { CreateTagDto } from './dto/create-tag.dto';
import { UpdateTagDto } from './dto/update-tag.dto';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';

@Injectable()
export class TagService {
  constructor(@InjectModel('Tag') private tagModel: Model<CreateTagDto>) {}
  async create(createTagDto: CreateTagDto) {
    // return 'This action adds a new tag';
    try {
      const tag = new this.tagModel(createTagDto);
      return await tag.save();
    } catch (e) {
      return e;
    }
  }

  async findAll() {
    // return `This action returns all tag`;
    return await this.tagModel.find().exec();
  }

  async findOne(id: string) {
    // return `This action returns a #${id} tag`;
    try {
      return await this.tagModel.findById(id);
    } catch (error) {
      return error;
    }
  }

  async update(id: string, updateTagDto: UpdateTagDto) {
    // return `This action updates a #${id} tag`;
    try {
      // console.log(id)
      return await this.tagModel.findByIdAndUpdate(id, updateTagDto);
    } catch (error) {
      return error;
    }
  }

  async remove(id: string) {
    // return `This action removes a #${id} tag`;
    try {
      // console.log(id)
      return await this.tagModel.findByIdAndUpdate(id, { type: -1 });
    } catch (error) {
      return error;
    }
  }
}
